﻿using System;
using System.Collections.Generic;
using System.Threading;

namespace ebclang
{
    public class ToEnumerable<T> where T : IStreamableElement
    {
        public event Action<IEnumerable<T>> Result;
        private readonly Enumerable<T> _list = new Enumerable<T>();
        private Enumerator<T> _enumerator;

        public void Process(T streamElement)
        {
            if (_enumerator == null)
            {
                var thread = new Thread(() =>
                                            {
                                                _enumerator = (Enumerator<T>)_list.GetEnumerator();
                                                _enumerator.AddElement(streamElement);
                                                Result(_list);
                                            });
                thread.Start();
            }
            else
                _enumerator.AddElement(streamElement);
        }
    }
}